package com.wxandroid.waveview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DrawFilter;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2016/12/16.
 */

public class WaveView extends View {

    //初始化路径
    private Path abovePath;
    private Path belowPath;
    //初始化画笔
    private Paint mAbovePaint;
    private Paint mBelowPaint;

    private DrawFilter mDrawFilter;
    private OnWaveAnimationListener waveAnimationListener;

    private float X;

//    public WaveView(Context context) {
//        this(context, null);
//    }

    public WaveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //初始化路径
        abovePath = new Path();
        belowPath = new Path();
        //初始化画笔  上
        mAbovePaint = new Paint(Paint.ANTI_ALIAS_FLAG);     //Paint.ANTI_ALIAS_FLAG  抗锯齿
        mAbovePaint.setAntiAlias(true);                    //抗锯齿
        mAbovePaint.setStyle(Paint.Style.FILL);
        mAbovePaint.setColor(Color.WHITE);
        //初始化画笔  下
        mBelowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBelowPaint.setAntiAlias(true);
        mBelowPaint.setStyle(Paint.Style.FILL);
        mBelowPaint.setColor(Color.WHITE);
        mBelowPaint.setAlpha(80);
        //画布抗锯齿
        mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        canvas.setDrawFilter(mDrawFilter);

        abovePath.reset();
        belowPath.reset();
        X -= 0.1f;
        float y, y2;

        double w = 2 * Math.PI / getWidth();

        abovePath.moveTo(getLeft(), getBottom());
        belowPath.moveTo(getLeft(), getBottom());
        for (float x = 0; x <= getWidth(); x += 40) {
            /**
             *  y=Asin(ωx+φ)+k
             *  A—振幅越大，波形在y轴上最大与最小值的差值越大
             *  ω—角速度， 控制正弦周期(单位角度内震动的次数)
             *  φ—初相，反映在坐标系上则为图像的左右移动。这里通过不断改变φ,达到波浪移动效果
             *  k—偏距，反映在坐标系上则为图像的上移或下移。
             */
            y = (float) (18 * Math.cos(w * x + X) + 6);
            y2 = (float) (18 * Math.sin(w * x + X));
            abovePath.lineTo(x, y);
            belowPath.lineTo(x, y2);
            //回调 把y坐标的值传出去(在activity里面接收让小机器人随波浪一起摇摆)
            waveAnimationListener.OnWaveAnimation(y);
        }
        abovePath.lineTo(getRight(), getBottom());
        belowPath.lineTo(getRight(), getBottom());

        canvas.drawPath(abovePath, mAbovePaint);
        canvas.drawPath(belowPath, mBelowPaint);

        postInvalidateDelayed(20);
    }

    public void setWaveAnimationListener(OnWaveAnimationListener waveAnimationListener) {
        this.waveAnimationListener = waveAnimationListener;
    }

    public interface OnWaveAnimationListener {
        void OnWaveAnimation(float y);
    }

}
